package fr.pud.client.controller;

import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JTextField;

/**
 * A controller which color the background of the input area if the input is
 * right
 * or wrong. Green if right, Orange if wrong.
 * Apr 9, 2012 - OSE.
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         InputCheck.java
 */
public class InputCheck implements KeyListener, FocusListener {
    /**
     * The regex which allows all the characters.
     */
    public static final String ALL_CHARACTER = ".+";
    /**
     * The regex which allows only the integer.
     */
    public static final String INTEGER_ONLY  = "[1-9][0-9]*";
    /**
     * The regex which allows only the real numbers.
     */
    public static final String NUMBER_ONLY   = INTEGER_ONLY + "(.[0-9]+)?";
    /**
     * The regex which allows only the hours.
     */
    public static final String HOURS         = "(([0-1]){0,1}([0-9]{1})|(2[0-3]))(h|:)([0-5]{1}[0-9]{1})";
    /**
     * The text field which should be check
     */
    private JTextField         textField;
    /**
     * A regular expression given by Strings
     */
    private String             regex;

    /**
     * The default constructor
     * @param aJTextField
     *            the text field to check
     * @param aRegex
     *            the pattern.
     */
    public InputCheck(JTextField aJTextField, String aRegex) {
        this.textField = aJTextField;
        this.regex = aRegex;
    }

    /**
     * Add the color to the background of the textfield according to the
     * pattern.
     */
    private void colorize() {
        if (this.textField.getText().matches(this.regex)) {
            this.textField.setBackground(Color.GREEN);
        }
        else {
            this.textField.setBackground(Color.ORANGE);
        }
    }

    /*
     * (non-Javadoc)
     * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
     */
    @Override
    public void focusGained(FocusEvent aArg0) {
        this.colorize();
    }

    /*
     * (non-Javadoc)
     * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
     */
    @Override
    public void focusLost(FocusEvent aArg0) {
        this.colorize();
    }

    /*
     * (non-Javadoc)
     * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
     */
    @Override
    public void keyPressed(KeyEvent aE) {
        this.colorize();
    }

    /*
     * (non-Javadoc)
     * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
     */
    @Override
    public void keyReleased(KeyEvent aE) {
        this.colorize();
    }

    /*
     * (non-Javadoc)
     * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
     */
    @Override
    public void keyTyped(KeyEvent aE) {
        this.colorize();
    }
}
